package com.lcxw.hxzbapi.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lcxw.hxzbapi.dao.mapper.UnionMemberMapper;
import com.lcxw.hxzbapi.entity.pojo.UnionMember;
import com.lcxw.hxzbcommon.common.StringUtil;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;

@Component
public class UnionMemberService {

    @Resource
    UnionMemberMapper unionMemberMapper;

    public int insert(UnionMember unionMember) {
        return unionMemberMapper.insert(unionMember);
    }

    public int deleteByUserId(long userId) {
        UpdateWrapper<UnionMember> wrapper = new UpdateWrapper<>();
        wrapper.eq("userId", userId);
        return unionMemberMapper.delete(wrapper);
    }

    public int deleteById(int id) {
        return unionMemberMapper.deleteById(id);
    }

    /**
     * 获取已加入工会的成员
     *
     * @return
     */
    public IPage<UnionMember> getUnionMembers(int unionId, Long userId,
        int pageNum, int pageSize) {

        IPage<UnionMember> iPage = new Page<>(pageNum, pageSize);
        QueryWrapper<UnionMember> wrapper = new QueryWrapper<>();
        wrapper.eq("unionId", unionId);
        wrapper.eq(userId != null, "userId", userId);
        return unionMemberMapper.selectPage(iPage, wrapper);
    }

    public List<UnionMember> getAllMembers(int unionId) {
        QueryWrapper<UnionMember> wrapper = new QueryWrapper<>();
        wrapper.eq("unionId", unionId);
        return unionMemberMapper.selectList(wrapper);
    }

    public UnionMember selectByUserId(long userId) {
        QueryWrapper<UnionMember> wrapper = new QueryWrapper<>();
        wrapper.eq("userId", userId);
        return unionMemberMapper.selectOne(wrapper);
    }

    public int update(UnionMember unionMember) {
        return unionMemberMapper.updateById(unionMember);
    }

    public UnionMember selectById(int id) {
        return unionMemberMapper.selectById(id);
    }

}
